﻿var spa = spa || {};

spa.dataContext = function (bufferDefault) {
    /// <summary>
    /// Creates a DataContext to contain DataSets
    /// </summary>
    /// <param name="bufferDefault" type="Boolean">
    /// Optional: buffer changes for new DataSets (default: false)
    /// </param>
    /// <return>
    /// new DataContext
    /// </return>
    this.buffer = bufferDefault || false;
};

spa.dataContext.prototype = {
    AddSet: function (controllerName, keyPropertyName, buffer) {
        /// <summary>
        /// Add a New DataSet to the DataContext
        /// </summary>
        /// <param name="controllerName" type="String">
        /// Required: Name of the Web API Controller
        /// </param>
        /// <param name="keyPropertyName" type="String">
        /// Required: Key field (only 1 supported) for the entities
        /// </param>
        /// <param name="buffer" type="Boolean">
        /// Optional: buffer changes for new DataSets (default: contextDefault)
        /// </param>
        /// <return>
        /// new DataSet
        /// </return>
        if (!this[controllerName]) {
            this[controllerName] = new spa.dataSet(controllerName, keyPropertyName, this.buffer, this);
        }
        return this[controllerName];
    },
    FindEntity: function (controllerName, Key) {
        /// <summary>
        /// Find an Entity somewhere in the DataContext
        /// </summary>
        /// <param name="controllerName" type="String">
        /// Required: Name of the DataSet / Controller
        /// </param>
        /// <param name="key" type="var">
        /// Required: Key value of the entity
        /// </param>
        /// <return>
        /// dataEntity (if in collection)
        /// </return>
        return this[controllerName].FindByKey(key);
    }
};
